import baseapi from '@/api/base'
import { getDataByParameter, getObjetInfoByPid, getCountByParameter, saveObjetInfo, deleteObjetInfo } from '@/utils/baserequest'
import storage from 'store'

const api = {
  personServer: '/person',
  findPersonByIdno: '/person/by/idno/',
  orgMemberServer: '/relationOrgPerson',
  orgMembersList: '/relationOrgPerson/by/orgPid/',
  orgMembersCount: '/relationOrgPerson/countBy/orgPid/',
  deleteOrgMembers: '/relationOrgPerson/',
  // orgPid
  orgPid: storage.get('org')
}

export default api

// 得到单位成员 分页
export function getOrgMembersByOrgPid (headers, pageSize, current) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org'),
    authOption: '1'
  }
  const parameters = {
    orgPid: storage.get('org'),
    pageSize: pageSize,
    currentPage: current,
    sidx: 'empno'
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.orgMembersList
  const allheaders = Object.assign(headers, appIdHeaders)
  return getDataByParameter(url, parameters, allheaders)
  // .then(res => {
  //   return res
  // })
}

// 得到单位成员总数
export function getOrgMembersCountByOrgPid (headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org'),
    authOption: '1'
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.orgMembersCount
  const allheaders = Object.assign(headers, appIdHeaders)
  return getCountByParameter(url, appIdHeaders.org, allheaders)
}

// 得到人员信息
export function getPersonInfoByPersonPid (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.personServer
  const allheaders = Object.assign(headers, appIdHeaders)
  return getObjetInfoByPid(url, parameter, allheaders)
}

// id == 0 add     post
// id != 0 update  put
// 保存人员信息
export function savePersonInfo (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org')
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.personServer + '/'
  const allheaders = Object.assign(headers, appIdHeaders)
  return saveObjetInfo(url, parameter, allheaders)
}

// 保存添加单位成员关系
export function addOrgMember (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org')
  }
  const data = {
    sysSeq: 0,
    pid: '',
    orgPid: storage.get('org'),
    sortNum: 99
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.orgMemberServer + '/'
  const allheaders = Object.assign(headers, appIdHeaders)
  return saveObjetInfo(url, Object.assign(parameter, data), allheaders)
}

// 删除单位和成员关系
export function delOrgMember (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app
  }
  const data = {
    orgPid: storage.get('org')
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.deleteOrgMembers
  const allheaders = Object.assign(headers, appIdHeaders)
  return deleteObjetInfo(url, Object.assign(parameter, data), allheaders)
}

// 更新工号
export function updateOrgMemberEmpno (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org')
  }
  const data = {
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.orgMemberServer + '/'
  const allheaders = Object.assign(headers, appIdHeaders)
  return saveObjetInfo(url, Object.assign(parameter, data), allheaders)
}

export function getPersonByIdno (idno, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org'),
    authOption: '_'
  }
  const parameter = {
    idno: idno
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.findPersonByIdno
  const allheaders = Object.assign(headers, appIdHeaders)
  return getDataByParameter(url, parameter, allheaders)
}
